/**
 * Created by ran on 03/01/2017.
 */
'use strict';
var React = require('react-native');
var networkManager = React.NativeModules.NetworkManager;

class NetworkManager {

    // constructor() {
    //     super();
    //     DEBUG = true;
    // }


    start() {
        return new Promise((fulfill, reject) => {
            networkManager.initWithCallback((error) => {
                if (error) {
                    reject(error);
                } else {
                    fulfill();
                }
            });
        });
    }


    /**
     * 在初始化这个组件的时候,只会把method='POST'的请求,重复请求多次。method='GET'不作这个处理
     * @param url           请求的url
     * @param params        请求的参数 in json
     * @param method        'GET' 'POST'
     * @returns {Promise}    失败error不为null
     */
    requestWith(url, params, method) {
        return new Promise((fulfill, reject) => {
            networkManager.requestWith(url, params, method, (error, response) => {
                if (error) {
                    reject(error);
                } else {
                    fulfill(response);
                }
            });
        });
    }

    post(url, json) {
        return new Promise((fulfill, reject) => {
            networkManager.post(url, json, (error, response) => {
                if (error) {
                    reject(error);
                } else {
                    fulfill(response);
                }
            });
        });
    }

    get(url) {
        return new Promise((fulfill, reject) => {
            networkManager.get(url, (error, response) => {
                if (error) {
                    reject(error);
                } else {
                    fulfill(response);
                }
            });
        });
    }

    patch(url, json) {
        return new Promise((fulfill, reject) => {
            networkManager.patch(url, json, (error, response) => {
                if (error) {
                    reject(error);
                } else {
                    fulfill(response);
                }
            });
        });
    }

    /**
     * 上传图片 POST
     * @param url
     * @param params
     * @param filePath 文件名格式: "userIcon"+_userid_timestamp.png/jpeg/jpg
     * @returns {Promise}
     */
    uploadWith(url, params, filePath) {
        console.log("uploadwith interface");
        return new Promise((fulfill, reject) => {
            console.log("uploadwith interface promise:");
            console.log('url', url);
            console.log('params', params);
            console.log('filePath', filePath);
            networkManager.uploadWith(url, params, filePath, (error, response) => {
                if (error) {
                    reject(error);
                } else {
                    fulfill(response);
                }
            });
        });
    }


    exec(funcName, params) {
        var jscode = 'this.' + funcName.name + '(';
        for (var i = 0; i < params.length; i++) {
            console.log(typeof(params[i]) + params[i]);
            if (typeof(params[i]) == 'string') {
                jscode = jscode + "\"" + params[i] + "\"";
            } else {
                console.log(JSON.stringify(params[i]));
                jscode += JSON.stringify(params[i]);
            }
            if (i < params.length - 1) {
                jscode += ', ';
            }
        }
        jscode += ');';
        //if(DEBUG) {
        var start_date = new Date();
        console.log('执行前------------' + jscode);
        //}
        var result = eval(jscode);
        result.then((response) => {
            var end_date = new Date();
            console.log('执行后------------' + response);
            console.log('耗时  ------------' + (end_date.getTime() - start_date.getTime()) + '毫秒');
        }, (error) => {
            console.log('执行后------------' + error);
            console.log('耗时  ------------' + (end_date.getTime() - start_date.getTime()) + '毫秒');
        });
        return result;
    }
}

Date.prototype.format = function () {
    return this.getFullYear() + '-' +
        (this.getMonth() + 1 > 9 ? this.getMonth() + 1 : '0' + (this.getMonth() + 1)) + '-' +
        (this.getDate() > 9 ? this.getDate() : '0' + (this.getDate())) + ' ' +
        (this.getHours() > 9 ? this.getHours() : '0' + (this.getHours())) + ':' +
        (this.getMinutes() > 9 ? this.getMinutes() : '0' + (this.getMinutes())) + ':' +
        (this.getSeconds() > 9 ? this.getDate() : '0' + (this.getSeconds()));
};

Date.prototype.format1 = function () {
    return this.getFullYear() + '-' +
        (this.getMonth() + 1 > 9 ? this.getMonth() + 1 : '0' + (this.getMonth() + 1)) + '-' +
        (this.getDate() > 9 ? this.getDate() : '0' + (this.getDate()))
};


module.exports = new NetworkManager();